const app=getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    open:false,
    shop:{},
    backshop:{},
    shop1:false,
    shop2:false,
    theme:app.theme,
    shopid:''
  },
  // 当切换模式改变
  attached(){
    this.into();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    into(){
      this.setData({
        theme:app.theme
      })
    },
    cutShop(e){
      let shop=app.functions.getLocalStorage("shop");
      if(e!=shop.id){
        this.setData({
          shop:shop,
          open:true
        },()=>{
          this.shopMember(e);
        })
      }
    },
    shopMember(e){
      let params={  
        shopId: e
      };
      app.api.shopMember(params,(response)=>{
        this.setData({
          backshop:response.data
        })
      },(e)=>{
        app.functions.showToast(e.msg);
      })
    },

    onChange1(e){
      let shopid=e.currentTarget.dataset.id;
      this.setData({
        shopid:shopid,
        shop1:this.data.shop1==false?true:false,
        shop2:false
      })
    },
    onChange2(e){
      let shopid=e.currentTarget.dataset.id;
      this.setData({
        shopid:shopid,
        shop1:false,
        shop2:this.data.shop2==false?true:false
      })
    },
    submit(){
      if(app.functions.isEmpty(this.data.shopid)){
        app.functions.showToast("请选择店铺!");
        return;
      }
      let user= app.functions.getLocalStorage("user");
      if(app.functions.isEmpty(user)){
        // 没有登录时绑定店铺
        let params={
          id: this.data.shopid
        };
        app.api.shopMsg(params,(response)=>{
          if (response.code == 500 && response.msg == "团长已经关闭") {
            app.functions.showToast("团长已关闭，请重新选择团长！");
            setTimeout(function(){
              wx.redirectTo({
              url: "/pages_subpack/pop_shop_address/pop_shop_address"
              });
            },2000)
          }
          if(response.data.serviceType==1){
            app.functions.showToast("团长已关闭，请重新选择团长！");
            setTimeout(function(){
              wx.redirectTo({
                url: "/pages_subpack/pop_shop_address/pop_shop_address"
              });
            },1000)
          }
          app.functions.setLocalStorage('shop',response.data);
          let pages = getCurrentPages(); //获取加载的页面
            let currentPage = pages[pages.length - 1]; //获取当前页面的对象
            let url = currentPage.route ;//当前页面url
            app.cache[url].onPullDownRefresh();
          this.setData({
            open:false
          })
          return;
        },(e)=>{
          app.functions.showToast(e.msg);
        })
      }else{
        this.shopBind();
      }
    },
    // 绑定店铺
    shopBind(){
      let user = app.functions.getLocalStorage("user");
      let params={
          memberId: user.id,
          teamId: this.data.shopid
      };
      app.api.shopBInd(params,(response)=>{
        
        user.upshopTid = this.data.shopid;
        user.centreId = response.data;
        app.functions.setLocalStorage("user",user);
        app.api.shopMsg({id: this.data.shopid},(res)=>{
          if (res.code == 500 && res.msg == "团长已经关闭") {
            app.functions.showToast("团长已关闭，请重新选择团长！");
            setTimeout(function(){
              wx.redirectTo({
              url: "/pages_subpack/pop_shop_address/pop_shop_address"
              });
            },2000)
            return;
          }
          if(response.data.serviceType==1){
            app.functions.showToast("团长已关闭，请重新选择团长！");
            setTimeout(function(){
              wx.redirectTo({
                url: "/pages_subpack/pop_shop_address/pop_shop_address"
              });
            },1000)
          }
          if (res.data) {
            const shop = res.data;
            app.functions.setLocalStorage('shop',shop);
            let pages = getCurrentPages(); //获取加载的页面
            let currentPage = pages[pages.length - 1]; //获取当前页面的对象
            let url = currentPage.route ;//当前页面url
            app.cache[url].onPullDownRefresh();
            // app.cache[url].onShow();
            this.setData({
              open:false
            })
          }
    
        },(e)=>{
          app.functions.showToast(e.msg);
        })
      },(e)=>{
        app.functions.showToast(e.msg);
      })

    }
  }
})
